package cn.book.system.controller;
import cn.book.basic.utils.AjaxResult;
import cn.book.basic.utils.PageList;
import cn.book.system.Service.IDicTypeService;
import cn.book.system.domain.DicType;
import cn.book.system.query.DicTypeQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description: 部门controller类
 * @Author: Mr.She
 * @Version: 1.0
 * @Date: 2022/6/23 21:42
 */
@RestController
@RequestMapping("/type")
public class DicTypeController {

    @Autowired
    private IDicTypeService dicTypeService;


    /**
    * @Title: loadById
    * @Description: 根据Id查询
    * @Author: YQN
    * @Version: 1.0
    * @Date:  2022/6/24 9:59
    * @Parameters: [id]
    * @Return cn.book.org.domain.DicType
    */
    @GetMapping("/{id}")
    public DicType loadById(@PathVariable("id") Long id) {
        return dicTypeService.loadById(id);
    }

    /**
     * @Title: loadAll
     * @Description: 查询所有
     * @Author: Mr.She
     * @Version: 1.0
     * @Date:  2022/6/24 9:35
     * @Parameters: []
     * @Return java.util.List<cn.itsource.org.domain.DicType>
     */
    @GetMapping// 请求地址：http://localhost:8080/department GET请求
    public List<DicType> loadAll(){
        return dicTypeService.loadAll();
    }

    /**
    * @Title: queryData
    * @Description: 分页查询
    * @Author: YQN
    * @Version: 1.0
    * @Date:  2022/6/24 10:12
    * @Parameters: [departmentQuery]
    * @Return cn.book.basic.utils.PageList<cn.book.org.domain.DicType>
     * @return cn.book.basic.utils.AjaxResult
    */
    @PostMapping // 可以直接访问
    public PageList<DicType> queryData(@RequestBody DicTypeQuery departmentQuery) {

            PageList<DicType> data = dicTypeService.queryData(departmentQuery);
            return data;
    }

    /**
    * @Title: remove
    * @Description: 根据Id删除
    * @Author: YQN
    * @Version: 1.0
    * @Date:  2022/6/24 9:59
    * @Parameters: [id]
    * @Return cn.book.basic.utils.AjaxResult
    */
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable("id") Long id) {
        try {
            dicTypeService.remove(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error();
        }
    }

    /**
    * @Title: saveOrUpdate
    * @Description: 新增或修改
    * @Author: YQN
    * @Version: 1.0
    * @Date:  2022/6/24 10:04
    * @Parameters: [department]
    * @Return cn.book.basic.utils.AjaxResult
    */
    @PutMapping
    public AjaxResult saveOrUpdate(@RequestBody DicType department) {
        try {
            if (department.getId() != null) {
                dicTypeService.update(department);
            } else {
                dicTypeService.save(department);
            }
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error();
        }

    }

    /**
     * @Title: batchDel
     * @Description: 批量删除
     * @Author: Mr.She
     * @Version: 1.0
     * @Date:  2022/6/25 9:47
     * @Parameters: [ids]
     * @Return cn.itsource.basic.utils.AjaxResult
     */
    @PatchMapping
    public AjaxResult batchDel(@RequestBody Long[] ids){
        try {
            dicTypeService.batchDel(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error();
        }
    }




}
